package com.dhcc.sds.base.function;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.cdc.busi.entity.*;
import com.dhcc.cdc.busi.service.*;
import com.dhcc.cdc.sys.cache.config.CdcConfigCache;
import com.dhcc.cdc.sys.entity.ConfigurationEntity;
import com.dhcc.cdc.sys.service.IConfigurationService;
import com.dhcc.cdc.sys.service.IDictionaryService;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.framework.util.SpringContextHolder;
import com.dhcc.sds.base.entity.SdsQcentityEntity;
import com.dhcc.sds.base.service.ISdsQcentityService;
import com.dhcc.sds.busi.entity.SdsQcincaseEntity;
import com.dhcc.sds.busi.service.ISdsQcincaseService;
import com.dhcc.sds.util.DateUtil;
import com.dhcc.sds.util.StrUtil;
import com.google.common.base.CharMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 *
 * 入组筛查函数
 * @ClassName: GetEntrySrv
 * @Description: 返回N为不符合，返回Y为符合
 * @author zhangdc
 * @date 2021-06-01 10:10:53
 */
@Component("getEntrySrv")
public class GetEntrySrv {

    /*@Autowired
    private ICdcEpisodeService cdcEpisodeService ;
    @Autowired
    private ICdcDiagnosisService cdcDiagnosisService ;
    @Autowired
    private ICdcDictionaryService cdcDictionaryService ;
    @Autowired
    private ICdcSurgeryService cdcSurgeryService ;
    @Autowired
    private IConfigurationService configurationService ;
    @Autowired
    private IDictionaryService dictionaryService ;*/

    static ICdcEpisodeService cdcEpisodeService = SpringContextHolder.getBean("cdcEpisodeService");
    static ICdcDiagnosisService cdcDiagnosisService = SpringContextHolder.getBean("cdcDiagnosisService");
    static ICdcDictionaryService cdcDictionaryService = SpringContextHolder.getBean("cdcDictionaryService");
    static ICdcSurgeryService cdcSurgeryService = SpringContextHolder.getBean("cdcSurgeryService");
    static IConfigurationService configurationService = SpringContextHolder.getBean("configurationService");
    static IDictionaryService dictionaryService = SpringContextHolder.getBean("dictionaryService");
    static ICdcEprService cdcEprService = SpringContextHolder.getBean("cdcEprService");
    static ICdcOrdexecService cdcOrdexecService = SpringContextHolder.getBean("cdcOrdexecService");
    static ICdcOrdmastService cdcOrdmastService = SpringContextHolder.getBean("cdcOrdmastService");
    static ISdsQcincaseService sdsQcincaseService = SpringContextHolder.getBean("sdsQcincaseService");
    static ISdsQcentityService sdsQcentityService = SpringContextHolder.getBean("sdsQcentityService");
    static ICdcTransferService cdcTransferService = SpringContextHolder.getBean("cdcTransferService");
    static ICdcLocationService cdcLocationService = SpringContextHolder.getBean("cdcLocationService");

    protected static final Logger logger = LoggerFactory.getLogger(GetEntrySrv.class);

    /**
     * 24小时内出院
     */
    public String in24hOut(Long aEpisodeID, String arg){
        String res = "N";
        CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectById(aEpisodeID);
        if (!StringUtils.isEmpty(cdcEpisodeEntity)) {
            Date admDate = cdcEpisodeEntity.getAdmDate();
            Date dishDate = cdcEpisodeEntity.getDishDate();
            if (StringUtils.isEmpty(dishDate)){
                return res;
            }
            Long dateCompareHour = DateUtil.getDateCompare(dishDate, admDate, "h");
            //离院方式术语集取值
            String dishTypeCode = "HDSD00.11.057";
            String sdDishTypeCode = CdcConfigCache.me().getValueByKey("SDS", "SDDishTypeCode");
            if (!StringUtils.isEmpty(sdDishTypeCode)){
                dishTypeCode = sdDishTypeCode;
            }
            // 获取指定术语值
            String dishType = "";
            EntityWrapper<CdcEprEntity> wrapper = new EntityWrapper<>();
            wrapper.eq("episode_id", aEpisodeID)
                    .eq("code", dishTypeCode)
                    .eq("is_active",1);
            CdcEprEntity cdcEprEntity = cdcEprService.selectOne(wrapper);
            if (CommonUtil.isNotEmpty(cdcEprEntity)){
                dishType = cdcEprEntity.getValue();
            }
            //默认24小时内出院
            Long h = 24l;
            if (dateCompareHour <= h) {
                res = "Y";
                if (!StringUtils.isEmpty(arg)) {
                    if (Objects.equals("TransOut", arg) && !dishType.contains("转院")) {
                        res = "N";
                    } else if (Objects.equals("NoOrderOut", arg) && Objects.equals("医嘱离院", dishType)) {
                        res = "N";
                    }
                }
            }
        }
        return res;
    }

    /**
     * 24小时内死亡
     */
    public String in24hDeath(Long aEpisodeID, String arg){
        String res = "N";
        CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectById(aEpisodeID);
        if (!StringUtils.isEmpty(cdcEpisodeEntity)){
            Date admDate = cdcEpisodeEntity.getAdmDate();
            Date deathDate = cdcEpisodeEntity.getDeathDate();
            Long h = 24l;
            if (!StringUtils.isEmpty(deathDate)){
                Long dateCompareHour = DateUtil.getDateCompare(deathDate, admDate, "h");
                if (dateCompareHour <= h){
                    res = "Y";
                }
            }
        }
        return res;
    }

    /**
     * 30天重复住院
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String inHos30Days(Long aEpisodeID, String arg){
        String res = "N";
        ///当次就诊入组单病种
        Wrapper<SdsQcincaseEntity> wpr = new EntityWrapper<>();
        wpr.eq("episode_id", aEpisodeID).ne("state", "O");
        SdsQcincaseEntity mrList = sdsQcincaseService.selectOne(wpr);
        if (mrList==null) {
            return res;
        }
        Long entityId = mrList.getEntityId();

        CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectById(aEpisodeID);
        //本次就诊时间
        Date curAdmDate = cdcEpisodeEntity.getAdmDate();
        if (!StringUtils.isEmpty(cdcEpisodeEntity)){
            //住院次数
            Integer admNums = cdcEpisodeEntity.getAdmNums();
            //就诊类型
            CdcDictionaryEntity dictionaryEntity = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_type", "I");
            //就诊状态
            CdcDictionaryEntity dictionaryEntity2 = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_status", "C");
            //患者主索引
            String patientCode = cdcEpisodeEntity.getPatientCode();
            if (!StringUtils.isEmpty(admNums) && (admNums > 1)){
                //根据患者主索引查询患者多次就诊记录
                List<CdcEpisodeEntity> cdcEpisodeEntities = cdcEpisodeService.selectList(
                        new EntityWrapper<CdcEpisodeEntity>()
                                .eq("patient_code", patientCode)
                                .eq("bus_adm_type_id", dictionaryEntity.getId())
                                .lt("adm_nums", admNums)
                                .orderBy("adm_date", false)
                );
                for (int i = 0 ; i < cdcEpisodeEntities.size(); i++){
                    Long preEpisodeId = cdcEpisodeEntities.get(i).getId();
                    //取消就诊
                    if (Objects.equals(dictionaryEntity2.getId(), cdcEpisodeEntities.get(i).getBusAdmStatusId())){
                        continue;
                    }
                    Date PreDishDate = cdcEpisodeEntities.get(i).getDishDate();
                    if (PreDishDate == null) {
                        continue;
                    }
                    //默认如果两次就诊时间已经超过一年，则不再判断重复
                    if (DateUtil.getDateCompare(curAdmDate, PreDishDate,"d") > 365) {
                        break;
                    }
                    // //如果没入单病种，不做重复入组筛查
                    Wrapper<SdsQcincaseEntity> preWpr = new EntityWrapper<>();
                    preWpr.eq("episode_id", preEpisodeId).ne("state", "O");
                    SdsQcincaseEntity preMrlist = sdsQcincaseService.selectOne(preWpr);
                    if (preMrlist==null) {
                        continue;
                    }
                    // 匹配是否为同一疾病重复上报单病种
                    if (Objects.equals(preMrlist.getEntityId(), entityId)) {
                        res = "Y";
                        break;
                    }
                }
            }
        }
        return res;
    }

    /**
     * 本季度重复住院
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String inHosQuarter(Long aEpisodeID, String arg){
        String res = "N";
        ///当次就诊入组单病种
        Wrapper<SdsQcincaseEntity> wpr = new EntityWrapper<>();
        wpr.eq("episode_id", aEpisodeID).ne("state", "O");
        SdsQcincaseEntity mrList = sdsQcincaseService.selectOne(wpr);
        if (mrList==null) {
            return res;
        }
        Long entityId = mrList.getEntityId();

        CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectById(aEpisodeID);
        //本次就诊时间
        Date curAdmDate = cdcEpisodeEntity.getAdmDate();
        if (!StringUtils.isEmpty(cdcEpisodeEntity)){
            //住院次数
            Integer admNums = cdcEpisodeEntity.getAdmNums();
            //就诊类型
            CdcDictionaryEntity dictionaryEntity = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_type", "I");
            //就诊状态
            CdcDictionaryEntity dictionaryEntity2 = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_status", "C");
            //患者主索引
            String patientCode = cdcEpisodeEntity.getPatientCode();
            if (!StringUtils.isEmpty(admNums) && (admNums > 1)){
                //根据患者主索引查询患者多次就诊记录
                List<CdcEpisodeEntity> cdcEpisodeEntities = cdcEpisodeService.selectList(
                        new EntityWrapper<CdcEpisodeEntity>()
                                .eq("patient_code", patientCode)
                                .eq("bus_adm_type_id", dictionaryEntity.getId())
                                .lt("adm_nums", admNums)
                                .orderBy("adm_date", false)
                );
                for (int i = 0 ; i < cdcEpisodeEntities.size(); i++){
                    Long preEpisodeId = cdcEpisodeEntities.get(i).getId();
                    //取消就诊
                    if (Objects.equals(dictionaryEntity2.getId(), cdcEpisodeEntities.get(i).getBusAdmStatusId())){
                        continue;
                    }
                    Date PreDishDate = cdcEpisodeEntities.get(i).getDishDate();
                    if (PreDishDate == null) {
                        continue;
                    }
                    // //如果没入单病种，不做重复入组筛查
                    Wrapper<SdsQcincaseEntity> preWpr = new EntityWrapper<>();
                    preWpr.eq("episode_id", preEpisodeId).ne("state", "O");
                    SdsQcincaseEntity preMrlist = sdsQcincaseService.selectOne(preWpr);
                    if (preMrlist==null) {
                        continue;
                    }
                    // 匹配是否为同一疾病重复上报单病种
                    if (CommonUtil.notEquals(preMrlist.getEntityId(), entityId)) {
                        continue;
                    }
                    // 判断是否为同一年同一季度
                    if (com.dhcc.core.framework.util.DateUtil.isSameQuarter(curAdmDate, PreDishDate)) {
                        res = "Y";
                        break;
                    }
                }
            }
        }
        return res;
    }

    /**
     * 非首次确诊
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String notFirstDiag(Long aEpisodeID, String arg){
        String res = "N";
        //根据数据中心字典对照获取病案编目
        CdcDictionaryEntity diagSource = dictionaryService.getMatchCtDic("SDS", "SDMatchCdcDic", "F");
        //查询病案编目诊断信息
        List<CdcDiagnosisEntity> diagnosisEntities = cdcDiagnosisService.selectDiagInfo(aEpisodeID, diagSource.getId(), null);
        String CurrMDiagCode = "";
        for (CdcDiagnosisEntity cdcDiagnosisEntity : diagnosisEntities){
            if (Objects.equals(1, cdcDiagnosisEntity.getIsMain())){
                CurrMDiagCode = cdcDiagnosisEntity.getIcd10();
                break;
            }
        }
        //如果当次主要诊断没取到，则退出
        if (StringUtils.isEmpty(CurrMDiagCode)){
            return res;
        }

        CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectById(aEpisodeID);
        if (!StringUtils.isEmpty(cdcEpisodeEntity)){
            //住院次数
            Integer admNums = cdcEpisodeEntity.getAdmNums();
            //就诊类型
            CdcDictionaryEntity dictionaryEntity = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_type", "I");
            //就诊状态
            CdcDictionaryEntity dictionaryEntity2 = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_status", "C");
            //患者主索引
            String patientCode = cdcEpisodeEntity.getPatientCode();
            if (!StringUtils.isEmpty(admNums) && (admNums > 1)){
                //根据患者主索引查询患者多次就诊记录
                List<CdcEpisodeEntity> cdcEpisodeEntities = cdcEpisodeService.selectList(
                        new EntityWrapper<CdcEpisodeEntity>()
                                .eq("patient_code", patientCode)
                                .eq("bus_adm_type_id", dictionaryEntity.getId())
                                .lt("adm_nums", admNums)
                                .orderBy("adm_date", false)
                );
                String beforeIcd10 = "";
                for (int i = 0 ; i < cdcEpisodeEntities.size(); i++){
                    //取消就诊
                    if (Objects.equals(dictionaryEntity2.getId(), cdcEpisodeEntities.get(i).getBusAdmStatusId())){
                        continue;
                    }
                    List<CdcDiagnosisEntity> beforeDiag = cdcDiagnosisService.selectDiagInfo(cdcEpisodeEntities.get(i).getId(), diagSource.getId(), null);
                    for (CdcDiagnosisEntity cdcDiagnosisEntity : beforeDiag){
                        if (Objects.equals(1, cdcDiagnosisEntity.getIsMain())){
                            beforeIcd10 = cdcDiagnosisEntity.getIcd10();
                            break;
                        }
                    }
                    if (Objects.equals(CurrMDiagCode, beforeIcd10)){
                        res = "Y";
                        break;
                    }
                }
            }
        }
        return res;
    }

    /**
     * 检查 TODO
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String pACInfo(Long aEpisodeID, String arg){
        String res = "N";
        return res;
    }

    /**
     * 临床试验 TODO
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String clinTest(Long aEpisodeID, String arg){
        String res = "N";
        return res;
    }

    /**
     * 门诊治疗无效 TODO
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String oPFailed(Long aEpisodeID, String arg){
        String res = "N";
        return res;
    }

    /**
     * 年龄
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String ageInfo(Long aEpisodeID, String arg){
        String res = "N";
        CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectById(aEpisodeID);
        if (!StringUtils.isEmpty(cdcEpisodeEntity)){
            boolean b = false;
            if (arg.contains("月")){
                if (StringUtils.isEmpty(cdcEpisodeEntity.getBirthdate()) ){
                    return res;
                }
                long month = cn.hutool.core.date.DateUtil.betweenMonth(cdcEpisodeEntity.getAdmDate(), cdcEpisodeEntity.getBirthdate(), true);
                arg = arg.replace("月", "");
                b = StrUtil.judgeGreaterOrLess(arg,  month);
            }else {
                String ageS=cdcEpisodeEntity.getAge();
                if (CommonUtil.isEmpty(ageS)) return res;
                int age=Integer.parseInt(ageS);
                b = StrUtil.judgeGreaterOrLess(arg, (long) age);
            }
            if (b){
                res = "Y";
            }
            // //根据出生日期计算年龄
            // Date birthdate = cdcEpisodeEntity.getBirthdate();
            // if (!StringUtils.isEmpty(birthdate)){
            //     int age = cn.hutool.core.date.DateUtil.ageOfNow(birthdate);
            //     if (!StringUtils.isEmpty(age) && !StringUtils.isEmpty(arg)){
            //         boolean b = StrUtil.judgeGreaterOrLess(arg, (long) age);
            //         if (b){
            //             res = "Y";
            //         }
            //     }
            // }
        }
        return res;
    }

    /**
     * 年龄（具体）
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String ageInfoDetail(Long aEpisodeID, String arg){
        String res = "";
        CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectById(aEpisodeID);
        if (!StringUtils.isEmpty(cdcEpisodeEntity)){
            boolean b = false;
            if (arg.contains("月")){
                if (StringUtils.isEmpty(cdcEpisodeEntity.getBirthdate()) ){
                    return res;
                }
                long month = cn.hutool.core.date.DateUtil.betweenMonth(cdcEpisodeEntity.getAdmDate(), cdcEpisodeEntity.getBirthdate(), true);
                arg = arg.replace("月", "");
                b = StrUtil.judgeGreaterOrLess(arg,  month);
            }else {
                String ageS=cdcEpisodeEntity.getAge();
                if (CommonUtil.isEmpty(ageS)) return res;
                int age=Integer.parseInt(ageS);
                b = StrUtil.judgeGreaterOrLess(arg, (long) age);
            }
            if (b){
                res = cdcEpisodeEntity.getAge() + "(年龄)";
            }
            // //根据出生日期计算年龄
            // Date birthdate = cdcEpisodeEntity.getBirthdate();
            // if (!StringUtils.isEmpty(birthdate)){
            //     int age = cn.hutool.core.date.DateUtil.ageOfNow(birthdate);
            //     if (!StringUtils.isEmpty(age) && !StringUtils.isEmpty(arg)){
            //         boolean b = StrUtil.judgeGreaterOrLess(arg, (long) age);
            //         if (b){
            //             res = "Y";
            //         }
            //     }
            // }
        }
        return res;
    }

    /**
     *  全部手术
     *  定义标准：范围用~连接，其他用,连接
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String operInfo(Long aEpisodeID, String arg){
        String res = "N";
        //病案编目手术来源
        String sourceType = dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "F1");
        //病案编目手术
        List<String> mOperType = new ArrayList<>();
        mOperType.add(dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "O/1"));// 常规手术
        mOperType.add(dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "O/2"));// 介入治疗手术

        List<CdcSurgeryEntity> cdcSurgeryEntities = cdcSurgeryService.getFrontPageICDAll(aEpisodeID, sourceType, mOperType);
        for (CdcSurgeryEntity cdcSurgeryEntity : cdcSurgeryEntities){
            String code = cdcSurgeryEntity.getCode();
            if (!StringUtils.isEmpty(code)){
                //检查code是否在参数定义范围内
                String s = checkICDArea(code, arg);
                if (Objects.equals("Y", s)){
                    res = "Y";
                    break;
                }
            }
        }
        return res;
    }



    /**
     *  主要手术
     *  定义标准：范围用~连接，其他用,连接
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String mainOperInfo(Long aEpisodeID, String arg){
        String res = "N";
        //病案编目手术来源
        String sourceType = dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "F1");
        //病案编目手术
        List<String> mOperType = new ArrayList<>();
        mOperType.add(dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "O/1"));// 常规手术
        mOperType.add(dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "O/2"));// 介入治疗手术

        List<CdcSurgeryEntity> cdcSurgeryEntities = cdcSurgeryService.getFrontPageICDAll(aEpisodeID, sourceType, mOperType);
        for (CdcSurgeryEntity cdcSurgeryEntity : cdcSurgeryEntities){
            if (!cdcSurgeryEntity.getXcode().contains("||1")) {
                continue;
            }
            String code = cdcSurgeryEntity.getCode();
            if (!StringUtils.isEmpty(code)){
                //检查code是否在参数定义范围内
                String s = checkICDArea(code, arg);
                if (Objects.equals("Y", s)){
                    res = "Y";
                    break;
                }
            }
        }
        return res;
    }

    /**
     *  主要手术（具体）
     *  定义标准：范围用~连接，其他用,连接
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String mainOperInfoDetail(Long aEpisodeID, String arg){
        String res = "";
        //病案编目手术来源
        String sourceType = dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "F1");
        //病案编目手术
        List<String> mOperType = new ArrayList<>();
        mOperType.add(dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "O/1"));// 常规手术
        mOperType.add(dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "O/2"));// 介入治疗手术

        List<CdcSurgeryEntity> cdcSurgeryEntities = cdcSurgeryService.getFrontPageICDAll(aEpisodeID, sourceType, mOperType);
        for (CdcSurgeryEntity cdcSurgeryEntity : cdcSurgeryEntities){
            if (!cdcSurgeryEntity.getXcode().contains("||1")) {
                continue;
            }
            String code = cdcSurgeryEntity.getCode();
            if (!StringUtils.isEmpty(code)){
                //检查code是否在参数定义范围内
                String s = checkICDArea(code, arg);
                if (Objects.equals("Y", s)){
                    res = cdcSurgeryEntity.getDescription() + "(手术)";
                    break;
                }
            }
        }
        return res;
    }

    /**
     * 主要操作
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String handleInfo(Long aEpisodeID, String arg) {
        String res = "N";
        // 来源-病案编目
        String sourceType = dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "F1");
        // 类型-操作
        List<String> mHandleType = new ArrayList<>();
        mHandleType.add(dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "H/1"));
        mHandleType.add(dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "H/2"));

        List<CdcSurgeryEntity> cdcSurgeryEntities = cdcSurgeryService.getFrontPageICDAll(aEpisodeID, sourceType, mHandleType);
        for (CdcSurgeryEntity cdcSurgeryEntity : cdcSurgeryEntities){
            String code = cdcSurgeryEntity.getCode();
            if (!StringUtils.isEmpty(code)){
                //检查code是否在参数定义范围内
                String s = checkICDArea(code, arg);
                if (Objects.equals("Y", s)){
                    res = "Y";
                    break;
                }
            }
        }
        return res;
    }

    /**
     * 主要操作（具体）
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String handleInfoDetail(Long aEpisodeID, String arg) {
        String res = "";
        // 来源-病案编目
        String sourceType = dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "F1");
        // 类型-操作
        List<String> mHandleType = new ArrayList<>();
        mHandleType.add(dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "H/1"));
        mHandleType.add(dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "H/2"));

        List<CdcSurgeryEntity> cdcSurgeryEntities = cdcSurgeryService.getFrontPageICDAll(aEpisodeID, sourceType, mHandleType);
        for (CdcSurgeryEntity cdcSurgeryEntity : cdcSurgeryEntities){
            String code = cdcSurgeryEntity.getCode();
            if (!StringUtils.isEmpty(code)){
                //检查code是否在参数定义范围内
                String s = checkICDArea(code, arg);
                if (Objects.equals("Y", s)){
                    res = cdcSurgeryEntity.getDescription() + "(操作)";
                    break;
                }
            }
        }
        return res;
    }

    /**
     * 孕期 TODO
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String pregnant(Long aEpisodeID, String arg){
        String res = "N";
        return res;
    }

    /**
     * 主要诊断
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String getDiagInfo(Long aEpisodeID, String arg){
        String res = "N";
        String diagICD = "";
        String diagDesc = "";
        //诊断数据来源 1:病历首页2:病案编目3:临床;
        //默认诊断数据来源为病案编目3
        String dataSource="2";
        if (Objects.equals("1", dataSource)){
            //TODO
        }else if (Objects.equals("2", dataSource)){
            //根据数据中心字典对照获取病案编目
            CdcDictionaryEntity diagSource = dictionaryService.getMatchCtDic("SDS", "SDMatchCdcDic", "F");
            //查询病案编目诊断信息
            List<CdcDiagnosisEntity> diagnosisEntities = cdcDiagnosisService.selectDiagInfo(aEpisodeID, diagSource.getId(), null);
            for (CdcDiagnosisEntity cdcDiagnosisEntity : diagnosisEntities){
                //主诊断
                if (Objects.equals(1, cdcDiagnosisEntity.getIsMain())){
                    diagICD = cdcDiagnosisEntity.getIcd10();
                    diagDesc = cdcDiagnosisEntity.getDescription();
                    if (!StringUtils.isEmpty(diagICD)){
                        //匹配诊断ICD是否符合准入标准
                        String s = checkICDArea(diagICD, arg);
                        if (Objects.equals("Y", s)){
                            res = "Y";
                            break;
                        }
                    }
                    if (CommonUtil.isNotEmpty(diagDesc)) {
                        //匹配诊断描述是否符合准入标准
                        String s = checkKeyArea(diagDesc, arg);
                        if (Objects.equals("Y", s)){
                            res = "Y";
                            break;
                        }
                    }
                }
            }
        }else if (Objects.equals("3", dataSource)){
            //TODO
        }

        return res;
    }

    /**
     * 主要诊断（具体）
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String getDiagInfoDetail(Long aEpisodeID, String arg){
        String res = "";
        String diagICD = "";
        String diagDesc = "";
        //诊断数据来源 1:病历首页2:病案编目3:临床;
        //默认诊断数据来源为病案编目3
        String dataSource="2";
        if (Objects.equals("1", dataSource)){
            //TODO
        }else if (Objects.equals("2", dataSource)){
            //根据数据中心字典对照获取病案编目
            CdcDictionaryEntity diagSource = dictionaryService.getMatchCtDic("SDS", "SDMatchCdcDic", "F");
            //查询病案编目诊断信息
            List<CdcDiagnosisEntity> diagnosisEntities = cdcDiagnosisService.selectDiagInfo(aEpisodeID, diagSource.getId(), null);
            for (CdcDiagnosisEntity cdcDiagnosisEntity : diagnosisEntities){
                //主诊断
                if (Objects.equals(1, cdcDiagnosisEntity.getIsMain())){
                    diagICD = cdcDiagnosisEntity.getIcd10();
                    diagDesc = cdcDiagnosisEntity.getDescription();
                    if (!StringUtils.isEmpty(diagICD)){
                        //匹配诊断ICD是否符合准入标准
                        String s = checkICDArea(diagICD, arg);
                        if (Objects.equals("Y", s)){
                            res = diagDesc + "(诊断)";
                            break;
                        }
                    }
                    if (CommonUtil.isNotEmpty(diagDesc)) {
                        //匹配诊断描述是否符合准入标准
                        String s = checkKeyArea(diagDesc, arg);
                        if (Objects.equals(diagDesc, s)){
                            res = diagDesc + "(诊断)";
                            break;
                        }
                    }
                }
            }
        }else if (Objects.equals("3", dataSource)){
            //TODO
        }

        return res;
    }
    
    /**
     * 全部诊断
     * @param aEpisodeID
     * @param arg
     * @return
     * 2022.10.18 根据最新国家平台回复，全部诊断包含主要诊断+第一次要诊断
     */
    public String getAllDiagInfo(Long aEpisodeID, String arg){
        String res = "N";
        String diagICD = "";
        String diagDesc = "";
        int icount = 99;
        if (CommonUtil.contains(arg, "#")) {
            String[] parts = arg.split("#");
            // 检查数组是否为空或分割结果为空
            if (parts != null && parts.length > 0) {
                icount = Integer.parseInt(parts[0]);
            }
        }
        //诊断数据来源 1:病历首页2:病案编目3:临床;
        //默认诊断数据来源为病案编目3
        String dataSource="2";
        if (Objects.equals("1", dataSource)){
            //TODO
        }else if (Objects.equals("2", dataSource)){
            //根据数据中心字典对照获取病案编目
            CdcDictionaryEntity diagSource = dictionaryService.getMatchCtDic("SDS", "SDMatchCdcDic", "F");
            //查询病案编目诊断信息
            List<CdcDiagnosisEntity> diagnosisEntities = cdcDiagnosisService.selectDiagInfo(aEpisodeID, diagSource.getId(), null);
            int i = 0;
            for (CdcDiagnosisEntity cdcDiagnosisEntity : diagnosisEntities){
                //if (i >= 2) { //2022.10.18 根据最新国家平台回复，全部诊断包含主要诊断+第一次要诊断
                if (i >= icount) { //2024.6.7 根据填报要求变化，全部诊断即检索全部诊断，第一 第二诊断，主要诊断+第一次要诊断 判断诊断个数根据参数#前的数字决定
                    break;
                }
                i++;
                diagICD = cdcDiagnosisEntity.getIcd10();
                diagDesc = cdcDiagnosisEntity.getDescription();
                if (!StringUtils.isEmpty(diagICD)){
                    //匹配诊断是否符合准入标准
                    String s = checkICDArea(diagICD, arg);
                    if (Objects.equals("Y", s)){
                        res = "Y";
                        break;
                    }
                }
                if (CommonUtil.isNotEmpty(diagDesc)) {
                    //匹配诊断描述是否符合准入标准
                    String s = checkKeyArea(diagDesc, arg);
                    if (Objects.equals("Y", s)){
                        res = "Y";
                        break;
                    }
                }
            }
        }else if (Objects.equals("3", dataSource)){
            //TODO
        }

        return res;
    }

    /**
     * 全部诊断（具体）
     * @param aEpisodeID
     * @param arg
     * @return
     * 2022.10.18 根据最新国家平台回复，全部诊断包含主要诊断+第一次要诊断
     */
    public String getAllDiagInfoDetail(Long aEpisodeID, String arg){
        String res = "";
        String diagICD = "";
        String diagDesc = "";
        int icount = 99;
        if (CommonUtil.contains(arg, "#")) {
            String[] parts = arg.split("#");
            // 检查数组是否为空或分割结果为空
            if (parts != null && parts.length > 0) {
                icount = Integer.parseInt(parts[0]);
            }
        }
        //诊断数据来源 1:病历首页2:病案编目3:临床;
        //默认诊断数据来源为病案编目3
        String dataSource="2";
        if (Objects.equals("1", dataSource)){
            //TODO
        }else if (Objects.equals("2", dataSource)){
            //根据数据中心字典对照获取病案编目
            CdcDictionaryEntity diagSource = dictionaryService.getMatchCtDic("SDS", "SDMatchCdcDic", "F");
            //查询病案编目诊断信息
            List<CdcDiagnosisEntity> diagnosisEntities = cdcDiagnosisService.selectDiagInfo(aEpisodeID, diagSource.getId(), null);
            int i = 0;
            for (CdcDiagnosisEntity cdcDiagnosisEntity : diagnosisEntities){
                //if (i >= 2) { //2022.10.18 根据最新国家平台回复，全部诊断包含主要诊断+第一次要诊断
                if (i >= icount) { //2024.6.7 根据填报要求变化，全部诊断即检索全部诊断，第一 第二诊断，主要诊断+第一次要诊断 判断诊断个数根据参数#前的数字决定
                    break;
                }
                i++;
                diagICD = cdcDiagnosisEntity.getIcd10();
                diagDesc = cdcDiagnosisEntity.getDescription();
                if (!StringUtils.isEmpty(diagICD)){
                    //匹配诊断是否符合准入标准
                    String s = checkICDArea(diagICD, arg);
                    if (Objects.equals("Y", s)){
                        res = diagDesc + "(诊断)";
                        break;
                    }
                }
                if (CommonUtil.isNotEmpty(diagDesc)) {
                    //匹配诊断描述是否符合准入标准
                    String s = checkKeyArea(diagDesc, arg);
                    if (Objects.equals("Y", s)){
                        res = diagDesc + "(诊断)";
                        break;
                    }
                }
            }
        }else if (Objects.equals("3", dataSource)){
            //TODO
        }

        return res;
    }

    /**
     * 住院日
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String inHosDays(Long aEpisodeID, String arg){
        String res = "N";
        CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectById(aEpisodeID);
        if (!StringUtils.isEmpty(cdcEpisodeEntity)) {
            Date admDate = cdcEpisodeEntity.getAdmDate();
            Date dishDate = cdcEpisodeEntity.getDishDate();
            if (StringUtils.isEmpty(dishDate)){
                dishDate = new Date();
            }
            Long d = DateUtil.getDateCompare(dishDate, admDate, "d");
            boolean b = StrUtil.judgeGreaterOrLess(arg, d);
            if (b){
                res = "Y";
            }
        }
        return res;
    }

    /**
     * 转院
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String hosTransInfo(Long aEpisodeID, String arg){
        String res = "N";
        String transInfo = "";
        String value = "";
        String sdTransferCode = CdcConfigCache.me().getValueByKey("SDS", "SDTransferCode");
        if (!StringUtils.isEmpty(sdTransferCode)){
            value = sdTransferCode;
        }
        if (!StringUtils.isEmpty(value)){
            // 获取病案首页信息
            EntityWrapper<CdcEprEntity> wrapper = new EntityWrapper<>();
            wrapper.eq("episode_id", aEpisodeID)
                    .eq("code", value)
                    .eq("is_active",1);
            CdcEprEntity cdcEprEntity = cdcEprService.selectOne(wrapper);
            if (!StringUtils.isEmpty(cdcEprEntity)){
                transInfo = cdcEprEntity.getValue();
            }
            //1急诊2门诊3转入9其他
            if (Objects.equals("3", transInfo)){
                res = "Y";
            }
        }
        return res;
    }

    /**
     * 感染信息
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String infMsg(Long aEpisodeID, String arg){
        String res = "N";
        String value = "";
        // 入院病情代码术语集取值
        String admissionConditionCode = "HDSD00.22.025";
        String sdAdmissionConditionCode = CdcConfigCache.me().getValueByKey("SDS", "SDAdmissionConditionCode");
        if (!StringUtils.isEmpty(sdAdmissionConditionCode)){
            admissionConditionCode = sdAdmissionConditionCode;
        }
        // 获取病案首页信息 入院病情代码为4（无）
        EntityWrapper<CdcEprEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("episode_id", aEpisodeID)
                .eq("code", admissionConditionCode)
                .eq("is_active",1);
        CdcEprEntity cdcEprEntity = cdcEprService.selectOne(wrapper);
        if (!StringUtils.isEmpty(cdcEprEntity)){
            value = cdcEprEntity.getValue();
        }
        //1有 2临床未确定 3情况不明 4无
        if (Objects.equals("4", value)){
            res = "Y";
        }

        return res;
    }

    /**
     * 次要诊断
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String oDIagInfo(Long aEpisodeID, String arg){
        String res = "N";
        //病案编目次要诊断
        String odiagType = dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "D2");
        //病案编目诊断
        String diagType = dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "F");
        //根据数据中心字典对照获取病案编目
        CdcDictionaryEntity diagSource = dictionaryService.getMatchCtDic("SDS", "SDMatchCdcDic", "F");
        //查询病案编目诊断信息
        List<CdcDiagnosisEntity> diagnosisEntities = cdcDiagnosisService.selectDiagInfo(aEpisodeID, diagSource.getId(), null);
        for (CdcDiagnosisEntity cdcDiagnosisEntity : diagnosisEntities){
            String s = checkICDArea(cdcDiagnosisEntity.getIcd10(), arg);
            if (Objects.equals("Y", s)){
                res = "Y";
                break;
            }
            String r = checkKeyArea(cdcDiagnosisEntity.getDescription(), arg);
            if (Objects.equals("Y", r)){
                res = "Y";
                break;
            }
        }
        return res;
    }

    /**
     * 医嘱
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String orderInfo(Long aEpisodeID, String arg){
        String res = "N";
        // 取得全部医嘱
        Wrapper<CdcOrdexecEntity> wpr = new EntityWrapper<>();
        wpr.eq("episode_id", aEpisodeID);
        List<CdcOrdexecEntity> orderList = cdcOrdexecService.selectList(wpr);
        for (CdcOrdexecEntity ordExec : orderList) {
            CdcOrdmastEntity ordMast = cdcOrdmastService.selectById(ordExec.getOrderMastId());
            String s = checkKeyArea(ordMast.getDescription(), arg);
            if (Objects.equals("Y", s)){
                res = "Y";
                break;
            }
        }
        return res;
    }

    /**
     * 医嘱（具体）
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String orderInfoDetail(Long aEpisodeID, String arg){
        String res = "N";
        // 取得全部医嘱
        Wrapper<CdcOrdexecEntity> wpr = new EntityWrapper<>();
        wpr.eq("episode_id", aEpisodeID);
        List<CdcOrdexecEntity> orderList = cdcOrdexecService.selectList(wpr);
        for (CdcOrdexecEntity ordExec : orderList) {
            CdcOrdmastEntity ordMast = cdcOrdmastService.selectById(ordExec.getOrderMastId());
            String s = checkKeyArea(ordMast.getDescription(), arg);
            if (Objects.equals("Y", s)){
                res = ordMast.getDescription() + "(医嘱)";
                break;
            }
        }
        return res;
    }


    /**
     * 已入病种
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String checkedEntity(Long aEpisodeID, String arg){
        String res = "N";
        String[] abbrevArr= arg.split(",");
        Wrapper<SdsQcincaseEntity> mrWpr = new EntityWrapper<>();
        mrWpr.eq("episode_id", aEpisodeID).ne("state", "O");
        List<SdsQcincaseEntity> mrList = sdsQcincaseService.selectList(mrWpr);

        if (CommonUtil.isEmpty(mrList)) return res;

        for (String abbrev : abbrevArr) {
            Wrapper<SdsQcentityEntity> spcWpr = new EntityWrapper<>();
            spcWpr.eq("abbrev", abbrev);
            SdsQcentityEntity sdsQcentityEntity = sdsQcentityService.selectOne(spcWpr);

            if (CommonUtil.isEmpty(sdsQcentityEntity)) continue;

            for (SdsQcincaseEntity entity : mrList) {
                if (entity.getEntityId().equals(sdsQcentityEntity.getId())){
                    res = "Y";
                    break;
                }
            }
        }


        return res;
    }

    /**
     * 入住ICU
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String inICU(Long aEpisodeID, String arg){
        String res = "N";
        String[] keywords = {"ICU", "重症", "监护"};
        //直接从转科记录取值
        List<CdcTransferEntity> cdcTransferEntities = cdcTransferService.selectList(new EntityWrapper<CdcTransferEntity>().eq("episode_id", aEpisodeID));
        if (CommonUtil.isNotEmpty(cdcTransferEntities)) {
            for (CdcTransferEntity cdcTransferEntity : cdcTransferEntities) {
                CdcLocationEntity cdcLocationEntity = cdcLocationService.selectById(cdcTransferEntity.getTranLocId());
                if (CommonUtil.isNotEmpty(cdcLocationEntity)) {
                    String locationName = cdcLocationEntity.getDescription();
                    for (String keyword : keywords) {
                        if (locationName.contains(keyword)) {
                            res = "Y";
                            break;
                        }
                    }
                }
            }
        }
        return res;
    }

    /**
     * 入住ICU（具体）
     * @param aEpisodeID
     * @param arg
     * @return
     */
    public String inICUDetail(Long aEpisodeID, String arg){
        String res = "";
        String[] keywords = {"ICU", "重症", "监护"};
        //直接从转科记录取值
        List<CdcTransferEntity> cdcTransferEntities = cdcTransferService.selectList(new EntityWrapper<CdcTransferEntity>().eq("episode_id", aEpisodeID));
        if (CommonUtil.isNotEmpty(cdcTransferEntities)) {
            for (CdcTransferEntity cdcTransferEntity : cdcTransferEntities) {
                CdcLocationEntity cdcLocationEntity = cdcLocationService.selectById(cdcTransferEntity.getTranLocId());
                if (CommonUtil.isNotEmpty(cdcLocationEntity)) {
                    String locationName = cdcLocationEntity.getDescription();
                    for (String keyword : keywords) {
                        if (locationName.contains(keyword)) {
                            res = locationName + "(入住ICU)";
                            break;
                        }
                    }
                }
            }
        }
        return res;
    }

    /**
     * 准入ICD范围检查方法
     * @param operCode
     * @param aOper
     * @return
     */
    public String checkICDArea(String operCode, String aOper){
        String res = "N";
        if (CommonUtil.isEmpty(operCode) || CommonUtil.isEmpty(aOper)) {
            return res;
        }
        if (CommonUtil.equals(operCode, aOper)) {
            return "Y";
        }
        aOper = aOper.replace("，", ",");
        aOper = aOper.replace("、", ",");
        aOper = aOper.replace("——", "~");
        aOper = aOper.replace("-", "~");
        if (operCode.contains("+")){
            String[] split = operCode.split("\\+");
            operCode = split[0];
        }

        if (!StringUtils.isEmpty(operCode) && !StringUtils.isEmpty(aOper)){
            if (aOper.contains(operCode) || operCode.indexOf(aOper)==0){
                //1.直接包含
                res = "Y";
            }else {
                if (operCode.contains(".")){
                    operCode = operCode.split("\\.")[0]+"."+operCode.split("\\.")[1].replaceAll("x", "0");
                }
                //定义operCode的字母和数字
                String a1 = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z')).retainFrom(operCode.split("\\.")[0]);
                String s3 = CharMatcher.inRange('0', '9').or(CharMatcher.is('.')).retainFrom(operCode);
                long l3 = StrUtil.strToLong(s3);
                //2.有范围查询是否在范围内
                if (aOper.contains("~") && !aOper.contains(",")){
                    //只有一个范围的情况
                    String[] split = aOper.split("~");
                    //字母获取
                    String a2 = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z')).retainFrom(split[0].split("\\.")[0]);
                    //数字获取
                    String s1 = CharMatcher.inRange('0', '9').or(CharMatcher.is('.')).retainFrom(split[0]);
                    String s2 = CharMatcher.inRange('0', '9').or(CharMatcher.is('.')).retainFrom(split[1]);
                    //字母判断
                    if (Objects.equals(a1, a2)){
                        long l1 = StrUtil.strToLong(s1);
                        long l2 = StrUtil.strToLong(s2);
                        if (l1 < l3 && l3 < l2){
                            res = "Y";
                        } else if (s3.indexOf(s1)==0||s3.indexOf(s2)==0) {
                            res = "Y";
                        }
                    }
                }else if (aOper.contains(",")){
                    //多个范围，满足一个退出
                    String[] split = aOper.split(",");
                    for (String s : split){
                        if (s.contains("~")){
                            String[] split1 = s.split("~");
                            //字母获取
                            String a2 = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z')).retainFrom(split1[0].split("\\.")[0]);
                            //数字获取
                            String s1 = CharMatcher.inRange('0', '9').or(CharMatcher.is('.')).retainFrom(split1[0]);
                            String s2 = CharMatcher.inRange('0', '9').or(CharMatcher.is('.')).retainFrom(split1[1]);
                            //字母判断
                            if (Objects.equals(a1, a2)){
                                long l1 = StrUtil.strToLong(s1);
                                long l2 = StrUtil.strToLong(s2);
                                if (l1 < l3 && l3 < l2){
                                    res = "Y";
                                    break;
                                }else if (s3.indexOf(s1)==0||s3.indexOf(s2)==0) {
                                    res = "Y";
                                    break;
                                }
                            }
                        } else {
                            //字母获取
                            String a2 = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z')).retainFrom(s.split("\\.")[0]);
                            //数字获取
                            if (s.contains(".")){
                                s = s.split("\\.")[0]+"."+s.split("\\.")[1].replaceAll("x", "0");
                            }
                            String s1 = CharMatcher.inRange('0', '9').or(CharMatcher.is('.')).retainFrom(s);
                            //字母判断
                            if (Objects.equals(a1, a2)) {
                                if (s3.indexOf(s1)==0) {
                                    res = "Y";
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    //字母获取
                    String a2 = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z')).retainFrom(aOper.split("\\.")[0]);
                    //数字获取
                    if (aOper.contains(".")){
                        aOper = aOper.split("\\.")[0]+"."+aOper.split("\\.")[1].replaceAll("x", "0");
                    }
                    String s1 = CharMatcher.inRange('0', '9').or(CharMatcher.is('.')).retainFrom(aOper);
                    //字母判断
                    if (Objects.equals(a1, a2)) {
                        if (s3.indexOf(s1)==0) {
                            res = "Y";
                        }
                    }
                }
            }
        }
        return res;
    }

    /**
     * 准入关键词检查方法
     * @param targetStr
     * @param keyWords
     * @return
     */
    public String checkKeyArea(String targetStr, String keyWords){
        String res = "N";
        if (CommonUtil.isEmpty(targetStr) || CommonUtil.isEmpty(keyWords)) {
            return res;
        }
        keyWords = keyWords.replace("，", ",");
        keyWords = keyWords.replace("、", ",");
        keyWords = keyWords.replace("——", "~");
        keyWords = keyWords.replace("-", "~");
        targetStr = targetStr.replace("，",",");
        targetStr = targetStr.replace("——", "~");
        targetStr = targetStr.replace("-","~");
        String[] split = keyWords.split(",");
        for (String s : split) {
            if (targetStr.contains(s)) {
                res = "Y";
                break;
            }
        }
        return res;
    }

    /**
     * 统一检查排除符合条件病种
     * 条件1：24小时内出院，除日间手术外
     * 条件2：同一病种31日又12小时内重复住院
     * @param aEpisodeID
     * @return
     */
    public String unifyCheck(Long aEpisodeID, String entityCode) {
        String reason = "";
        CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectById(aEpisodeID);
        if (!StringUtils.isEmpty(cdcEpisodeEntity)) {
            // 对于条件1，先按照病种配置来决定是否执行
            Boolean result = true;
            String sd24HourDel = CdcConfigCache.me().getValueByKey("SDS", "SD24HourDel");
            if (CommonUtil.isEmpty(sd24HourDel)) {
                result = false;
            } else {
                String[] sd24HourDelList = sd24HourDel.split(",");
                result = Arrays.asList(sd24HourDelList).contains(entityCode);
            }
            if (result) {
                //统一排除条件1，24小时内出院，日间手术除外
                Date admDate = cdcEpisodeEntity.getAdmDate();
                Date dishDate = cdcEpisodeEntity.getDishDate();
                if (StringUtils.isEmpty(dishDate)) {
                    return reason;
                }
                Long dateCompareHour = DateUtil.getDateCompare(dishDate, admDate, "h");
                //默认24小时内出院
                Long h = 24l;
                if (dateCompareHour <= h) {
                    List<CdcTransferEntity> transList = cdcTransferService.selectList(new EntityWrapper<CdcTransferEntity>().eq("episode_id", aEpisodeID));
                    int daySurgrry = 0;
                    for (CdcTransferEntity cdcTransferEntity : transList) {
                        if (cdcTransferEntity.getTranLocId() == null) continue;
                        CdcLocationEntity tranLocation = cdcLocationService.selectById(cdcTransferEntity.getTranLocId());
                        if (tranLocation.getDescription().contains("日间")) {
                            daySurgrry = 1;
                        }
                    }
                    if (daySurgrry == 0) {
                        reason = "24小时内出院的非日间手术患者";
                        return reason;
                    }
                }
            }

            //统一排除条件2，同一病种31日又12小时内重复住院
            ///当次就诊入组单病种
            Wrapper<SdsQcincaseEntity> wpr = new EntityWrapper<>();
            wpr.eq("episode_id", aEpisodeID).ne("state", "O");
            SdsQcincaseEntity mrList = sdsQcincaseService.selectOne(wpr);
            if (mrList==null) {
                return reason;
            }
            Long entityId = mrList.getEntityId();
            //本次就诊时间
            Date curAdmDate = cdcEpisodeEntity.getAdmDate();
            if (!StringUtils.isEmpty(cdcEpisodeEntity)){
                //住院次数
                Integer admNums = cdcEpisodeEntity.getAdmNums();
                //就诊类型
                CdcDictionaryEntity dictionaryEntity = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_type", "I");
                //就诊状态
                CdcDictionaryEntity dictionaryEntity2 = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_status", "C");
                //患者主索引
                String patientCode = cdcEpisodeEntity.getPatientCode();
                if (!StringUtils.isEmpty(admNums) && (admNums > 1)){
                    //根据患者主索引查询患者多次就诊记录
                    List<CdcEpisodeEntity> cdcEpisodeEntities = cdcEpisodeService.selectList(
                            new EntityWrapper<CdcEpisodeEntity>()
                                    .eq("patient_code", patientCode)
                                    .eq("bus_adm_type_id", dictionaryEntity.getId())
                                    .lt("adm_nums", admNums)
                                    .orderBy("adm_date", false)
                    );
                    for (int i = 0 ; i < cdcEpisodeEntities.size(); i++){
                        Long preEpisodeId = cdcEpisodeEntities.get(i).getId();
                        //取消就诊
                        if (Objects.equals(dictionaryEntity2.getId(), cdcEpisodeEntities.get(i).getBusAdmStatusId())){
                            continue;
                        }
                        Date PreDishDate = cdcEpisodeEntities.get(i).getDishDate();
                        if (PreDishDate == null) {
                            continue;
                        }
                        //默认如果两次就诊时间已经超过一年，则不再判断重复
                        if (DateUtil.getDateCompare(curAdmDate, PreDishDate,"d") > 90) {
                            break;
                        }
                        // //如果没入单病种，不做重复入组筛查
                        Wrapper<SdsQcincaseEntity> preWpr = new EntityWrapper<>();
                        preWpr.eq("episode_id", preEpisodeId).ne("state", "O");
                        SdsQcincaseEntity preMrlist = sdsQcincaseService.selectOne(preWpr);
                        if (preMrlist==null) {
                            continue;
                        }
                        // 匹配是否为同一疾病重复上报单病种
                        if (Objects.equals(preMrlist.getEntityId(), entityId)) {
                            //倒序找到相同病种记录，不管是否31日重复住院都可完成判断立即退出
                            if (DateUtil.getDateCompare(curAdmDate, PreDishDate,"h") < (31*24 + 12)) {
                                reason = "相同病种31日重复住院";
                            }
                            break;
                        }
                    }
                }
            }
        }
        return reason;
    }
}
